package com.mars.foodserver.config;

import com.mars.foodserver.constant.Constant;
import com.mars.foodserver.interceptor.AuthenticationInterceptor;
import com.mars.foodserver.service.ISysIgnorePathService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author wq
 * @version 1.0
 * @date 2020/12/29 11:03
 */
@Configuration
@Slf4j
@SuppressWarnings("all")
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private AuthenticationInterceptor tokenInterceptor;

    @Resource
    private ISysIgnorePathService sysIgnorePathService;

    @Resource
    private RedisTemplate redisTemplate;

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor).
                addPathPatterns("/**");
    }


    @PostConstruct
    public void cachePaths() {
        redisTemplate.delete(Constant.IGNORE_CACHE_PATH);
        BoundListOperations listOps = redisTemplate.boundListOps(Constant.IGNORE_CACHE_PATH);
        if (!redisTemplate.hasKey(Constant.IGNORE_CACHE_PATH)) {
            List<String> paths = sysIgnorePathService.acquirePaths();
            // 将整个列表存入redis中
            listOps.leftPushAll(paths);
        }
    }


}
